package com.kkb.kkbcommon.annotation.validation.validator;

import com.kkb.kkbcommon.annotation.validation.annotation.Ipv4Address;
import com.kkb.kkbcommon.basic.BaseUtil;
import com.kkb.kkbcommon.basic.RegexUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author: 朱明亮
 * @Description: 是否匹配IPV4地址
 * @Data Created in 2021-08-20 15:24:35
 * @Modified by:
 */
public class Ipv4AddressValidator implements ConstraintValidator<Ipv4Address, String> {
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return BaseUtil.haveEmpty(value) ? true : RegexUtil.isMatchByRegex(
                value,
                "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
    }
}
